import { Effect, Reducer } from 'umi';
import { _getInfo } from '@/api/index';
import { message } from 'antd';
import { UserInfo } from '@/type/index';
import updataState from '@/utils/modelPut';
export interface IndexModelState {
  userInfo: UserInfo;
}

export interface IndexModelType {
  namespace: 'index';
  state: IndexModelState;
  effects: {
    GET_INFO: Effect;
  };
  reducers: {
    save: Reducer<IndexModelState>;
  };
}
const IndexModel: IndexModelType = {
  namespace: 'index',
  state: {
    userInfo: {
      iat: 0,
      identity_id: '',
      identity_text: '',
      signTime: 0,
      user_id: '',
      user_name: '',
    },
  },
  effects: {
    //获取用户信息
    *GET_INFO({}, { call, put }) {
      let res = yield call(_getInfo);
      //用户信息获取成功
      if (res.data.code == 1) {
        yield put(updataState('save', { userInfo: res.data.data }));
      } else {
        message.error(res.data.msg);
      }
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};

export default IndexModel;
